package com.ljq.polymorphism;

/**
 * Description: 多态
 * Author: A
 * Date: 2025/2/24
 * Time: 16:03
 */
public class PolymorphismExample {
    public static void main(String[] args) {
        Animal cat = new Cat();
        Animal dog = new Dog();

        System.out.println( cat.name);  // 动物 多态弊端 只能访问父类的成员变量和方法 通过向下转型可以解决
        System.out.println( dog.name); // 动物
//        foo(cat);
//        foo(dog);
    }
    public static void foo(Animal a){
        if(a instanceof  Cat){
            Cat cat = (Cat) a; // 向下转型
            cat.climb();
        }   if(a instanceof  Dog){
            Dog dog = (Dog) a;
            dog.fetch();
        }
    }
}
